//
//  ZYCustomerFeedbackVC.m
//  YiLin
//
//  Created by Zyark on 2018/6/11.
//  Copyright © 2018年 Zyark. All rights reserved.
//

#import "ZYCustomerFeedbackVC.h"
#import "ZYTextView.h"
#import "ZYViewUtils.h"
#import "ZYFeedBackViewModel.h"

@interface ZYCustomerFeedbackVC ()
///<,UIPickerViewDataSource>
//@property(nonatomic,strong) UIPickerView *problemTypePickerView;
//@property(nonatomic,strong) UITextField *problemTypeField;
@property(nonatomic,strong) ZYTextView *problemTextView;
@property(nonatomic,strong) UITextField *emailField;
@property(nonatomic,strong) UIButton *confirmBtn;
@property(nonatomic,strong) NSArray *problemTypeArray;
@property(nonatomic,strong) ZYFeedBackViewModel *viewModel;
@end

@implementation ZYCustomerFeedbackVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setup];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.title = [[ZYChangeLanguage sharedZYChangeLanguage] getStringByKey:LC_ME_MAIN_CUSTOMER_FEEDBACK];
}

#pragma mark - init
-(void)setup{
    [self setupView];
    [self setupTap];
    [self bindView];
}
-(void)setupView{
    CGFloat margin = 10;
    ZYChangeLanguage *language = [ZYChangeLanguage sharedZYChangeLanguage];
//    _problemTypeField = [self setupTextField];
//    [self.view addSubview:_problemTypeField];
//    _problemTypeField.top = self.navigationView.bottom;
//    _problemTypeField.centerX = self.view.centerX;
//    _problemTypeField.text = [language getStringByKey:LC_CUSTOMER_FEEDBACK_LABEL_PROBLEM_TYPE];
//    [self setRightImageView:_problemTypeField imageName:@"ic_me_info_select"];
    
//    _problemTypePickerView = [[UIPickerView alloc] init];
//    //    problemPickerView.size = CGSizeMake(ZYSCREEN_W - left * 2, height);
//    _problemTypePickerView.backgroundColor = [UIColor whiteColor];
//    _problemTypePickerView.delegate = self;
//    _problemTypePickerView.dataSource = self;
//    _problemTypeField.inputView = _problemTypePickerView;
    
    _problemTextView = [[ZYTextView alloc] initWithFrame:CGRectMake(0, 0, ZYSCREEN_W - 30, 120)];
    [self.view addSubview:_problemTextView];
    _problemTextView.top = self.navigationView.bottom;
    _problemTextView.centerX = self.view.centerX;
    _problemTextView.placeholder = [NSString stringWithFormat:@"  %@",[language getStringByKey:LC_CUSTOMER_FEEDBACK_PLACEHOLDER_PROBLEM_DESCRIPTION]];
    _problemTextView.backgroundColor = RGBA(248, 248, 248, 1);
    
    _emailField = [self setupTextField];
    [self.view addSubview:_emailField];
    _emailField.top = _problemTextView.bottom + margin;
    _emailField.centerX = self.view.centerX;
    _emailField.placeholder = [language getStringByKey:LC_CUSTOMER_FEEDBACK_LABEL_EMAIL_OR_QQ];
    
    _confirmBtn = [ZYViewUtils setupConfirmBtn];
    [self.view addSubview:_confirmBtn];
    [_confirmBtn setTitle:[language getStringByKey:LC_CUSTOMER_FEEDBACK_BTN_SUBMIT] forState:UIControlStateNormal];
    _confirmBtn.centerX = self.view.centerX;
    _confirmBtn.top = _emailField.bottom + 25;
    [_confirmBtn addTarget:self action:@selector(confirmSubmit:) forControlEvents:UIControlEventTouchUpInside];
}

-(void)bindView{
    @weakify(self);
    [[self.viewModel.commitCommand.executionSignals switchToLatest] subscribeNext:^(id x) {
        @strongify(self);
        [[ToastHelper sharedToastHelper] toastSuccess:[[ZYChangeLanguage sharedZYChangeLanguage] getStringByKey:LC_COMMIT_SUCCESS]];
        [self.navigationController popViewControllerAnimated:YES];
    }];
    [self.viewModel.commitCommand.errors subscribeNext:^(id x) {
        @strongify(self);
        [ZYBasicHelper handleErrorMessaeWithError:x message:[NSErrorHelper handleErrorMessage:x] controller:self];
    }];
    //监听状态
    [[self.viewModel.commitCommand.executing skip:1] subscribeNext:^(id x) {
        BOOL executing = [x boolValue];
        [[ToastHelper sharedToastHelper] setSimleProgressVisiable:executing];
    }];
}

-(UITextField *)setupTextField{
    UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, ZYSCREEN_W - 30, 45)];
    textField.layer.cornerRadius = 5;
    textField.layer.masksToBounds = YES;
    textField.clearButtonMode = UITextFieldViewModeWhileEditing;
    textField.backgroundColor = RGBA(248, 248, 248, 1);
    textField.textColor = kLabelColor;
    textField.font = [UIFont systemFontOfSize:14];
    [self setLeftView:textField];
    return textField;
}

/**
 设置左视图
 @param textField texField
 */
-(void)setLeftView:(UITextField *)textField{
    UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 15, textField.height)];
    textField.leftViewMode = UITextFieldViewModeAlways;
    textField.leftView = leftView;
}

/**
 设置右视图
 @param textField texField
 @param imageName 显示的图片
 */
-(void)setRightImageView:(UITextField *)textField imageName:(NSString *)imageName{
    UIImageView *rightImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:imageName]];
    rightImageView.frame = CGRectMake(0, 0, 16, 10);
    UIView *rightView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ZYSCREEN_W * 0.08, ZYSCREEN_W * 0.26)];
    [rightView addSubview:rightImageView];
    rightImageView.centerY = rightView.centerY;
    textField.rightViewMode = UITextFieldViewModeAlways;
    textField.rightView = rightView;
}

#pragma mark UIPickerViewDelegate DataSource
////返回有几列
//-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
//    return 1;
//}
//
////返回指定列的行数
//-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
//    return [self.problemTypeArray count];
//}
//
//-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
//    return self.problemTypeArray[row];
//}
//
//
//-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
//    [_problemTypeField resignFirstResponder];
//    _problemTypeField.text = self.problemTypeArray[row];
//}

#pragma mark - Event
-(void)confirmSubmit:(UIButton *)button{
    [self dismissKeyboard];
    self.viewModel.desc = self.problemTextView.text;
    self.viewModel.link = self.emailField.text;
    [self.viewModel.commitCommand execute:nil];
}

#pragma mark - Getter
-(ZYFeedBackViewModel *)viewModel{
    if(!_viewModel){
        _viewModel = [[ZYFeedBackViewModel alloc] init];
    }
    return _viewModel;
}

@end
